/*
 * DownloadPanel.java
 *
 * Created on 04 November 2008, 11:43
 */
package fulltextarticledownloader.gui.panels;

import fulltextarticledownloader.beans.DownloadTask;
import fulltextarticledownloader.gui.DownloadingPropertyChangeHandler;
import fulltextarticledownloader.workers.AgentDownloadRunnable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  James Eales
 */
public class DownloadPanel extends javax.swing.JPanel {

    private ExecutorService pool;
    private DefaultTableModel downloadTableModel;
    private DownloadingPropertyChangeHandler messageHandler;
    private static final int DOWNLOAD_POOL_SIZE = 5;

    /** Creates new form DownloadPanel */
    public DownloadPanel() {
        pool = Executors.newFixedThreadPool(DOWNLOAD_POOL_SIZE);
        downloadTableModel = new DefaultTableModel(new String[]{"PubMed ID", "Current URL", "XML Status", "PDF Status", "Plain Text Status"}, 0);
        messageHandler = new DownloadingPropertyChangeHandler(downloadTableModel);
        initComponents();
        downloadTable.getColumnModel().getColumn(0).setPreferredWidth(100);
        downloadTable.getColumnModel().getColumn(1).setPreferredWidth(445);
        downloadTable.getColumnModel().getColumn(2).setPreferredWidth(120);
        downloadTable.getColumnModel().getColumn(3).setPreferredWidth(120);
        downloadTable.getColumnModel().getColumn(4).setPreferredWidth(120);
    }

    public synchronized void addDownloadTask(DownloadTask task) {
        for (String string : task.getIds()) {
            downloadTableModel.addRow(new String[]{string,"","Waiting","Waiting","Waiting"});
            int row = downloadTableModel.getRowCount();
            if (row == 0) {
                row = 1;
            }
            AgentDownloadRunnable downloadJob = new AgentDownloadRunnable(row-1,string, task.getStoreHere(), task.isSavePDF(), task.isSavePlainText(),task.isSaveXML(),task.isCheckPDF());
            downloadJob.addPropertyChangeListener(messageHandler);
            pool.execute(downloadJob);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        downloadTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        downloadTable.setModel(downloadTableModel);
        downloadTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
        jScrollPane1.setViewportView(downloadTable);

        jLabel1.setText("Download Progress");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable downloadTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
